#! /usr/bin/env python
#coding=utf-8

from get_month_quarter import *
from get_manage_requirement import *
#from __future__ import division


#调用get_month_quarter.py中get_db_connection()获取数据库联接
db = get_db_connection()

#meta = MetaData(db)
#db.metadata.reflect()

def run():

#    first_day,last_day = get_week()
    first_day,last_day,month_view = get_month()
    report_type = '2'
    
#    report_type = '1'
    data =  get_manage_requirement(first_day,last_day,report_type)

    #print last_day,first_day

    req_model = get_model('manage_requirement_week')

    req_model.remove(and_(req_model.c.report_date == last_day,req_model.c.report_type == report_type))
    for v,t in data.items():
        #print 't',t,'v',v
        r = req_model(**t)
        r.save()
                        


if __name__ == '__main__':
    try:
        db.begin()
        run()
        db.commit()
        log.info("finish")
    except Exception, e:
        db.rollback()
        log.exception(e)
        log.error('error')
        raise
